import org.devops.git
import org.devops.dingmes
import org.devops.mvnbuild
import org.devops.dockerbuild
import org.devops.deploy

def call(Map map) {
    def private g = new git()
    def private mb = new mvnbuild()
    def private dm = new dingmes()
    def private dp = new deploy()

    def deploy_path = map.apppath
    def ansible_host = map.ansible_host + '-' + params.ENV_TYPE
    pipeline {
        agent {
            kubernetes {
                inheritFrom 'maven-3.6'
            }
        }
        // agent any
        parameters {
            string(name: 'PROJECT_NAME',
                defaultValue:  map.project_name,
                description: '项目名称')

            choice(name: 'ENV_TYPE',
                choices: ['dev', 'test', 'pro', 'pre'],
                description: '部署环境：dev:开发环境; test:测试环境; pre:预发布环境; pro: 生产环境')

            string(name: 'PROJECT_URL',
                defaultValue: map.git_url,
                description: '项目仓库地址')

            gitParameter(
                    defaultValue: map.git_default_branch,
                    branchFilter: 'origin/(.*)',
                    name: 'BRANCH_TAG',
                    quickFilterEnabled: true,
                    selectedValue: 'DEFAULT',
                    sortMode: 'DESCENDING_SMART',
                    tagFilter: '*',
                    type: 'PT_BRANCH',
                    description: 'Please select a branch or tag to build',
                    useRepository: map.git_url
            )
        }

        stages {
            stage('git clone') {
                steps {
                    script {
                        BRANCH_TAG = sh(returnStdout: true, script: "echo ${params.BRANCH_TAG}|awk -F '/' '{print \$NF}'").trim()
                        g.gitco(params.BRANCH_TAG, map.git_credentials_id, map.git_url)
                    }
                }
            }

            stage('maven build') {
                steps {
                    container('maven') {
                        script {
                            echo "[INFO] 执行maven构建 构建环境$ENV_TYPE"
                            if ( map.mvn_param == '') {
                                mb.mavencloudbuild(map.mvn_param)
                            }
                            if ( map.mvn_dev_param == '-Ptest' &&  ENV_TYPE == 'dev' ) {
                                mb.mavencloudbuild(map.mvn_dev_param)
                            }
                            if ( map.mvn_test_param == '-Ptest' &&  ENV_TYPE == 'test' ) {
                                mb.mavencloudbuild(map.mvn_test_param)
                            }
                            if ( map.mvn_pre_param == '-Pbeta' &&  ENV_TYPE == 'pre' ) {
                                mb.mavencloudbuild(map.mvn_pre_param)
                            }
                            if ( map.mvn_pro_param == '-Pproduction' &&  ENV_TYPE == 'pro' ) {
                                mb.mavencloudbuild(map.mvn_pro_param)
                            }
                        }
                    }
                }
                post {
                    always {
                    archiveArtifacts artifacts: 'workspace/**/target/*.jar', fingerprint: true
                    // junit 'build/reports/**/*.xml'
                    }
                }
            }
            stage('deploy') {
                parallel {
                    stage('deploy dev') {
                        // 测试环境部署
                        when {
                            expression { params.ENV_TYPE ==~ /(dev)/ }
                        // expression { BRANCH_TAG ==~ /(test)/ }
                        }
                        steps {
                            script {
                                echo "[INFO] dev 部署方式: $map.dev_deploy"
                                if ( map.dev_deploy == 'k8s' ) {
                                    dp.k8sDeploy(map.k8s_conn_yaml, params.ENV_TYPE, image_name, map.deploy_yaml)
                                }
                                if ( map.dev_deploy == 'ansible' ) {
                                    container('ansible') {
                                        dp.ansibleMvnDeploy(map.ansible_hosts_file, ansible_host, image_name, deploy_path)
                                    }
                                }
                                if ( map.dev_deploy == 'ssh' ) {
                                    dp.sshDockerDeploy(map.ssh_name, map.ssh_host, map.ssh_port, map.ssh_cred, map.dhub_cred, map.registry, image_name, deploy_path)
                                }
                                if ( map.dev_deploy == 'nodeploy' ) {
                                    throw new Exception('您选择的环境未配置部署方式！！！,请联系运维人员处理')
                                }
                            }
                        }
                    }
                    stage('deploy test') {
                        // 测试环境部署
                        when {
                            // expression { params.ENV_TYPE == "test" }
                            expression { params.ENV_TYPE ==~ /(test)/ }
                        }
                        steps {
                            script {
                                echo "[INFO] test 部署方式: $map.test_deploy"
                                if ( map.test_deploy == 'k8s' ) {
                                    dp.k8sDeploy(map.k8s_conn_yaml, params.ENV_TYPE, image_name, map.deploy_yaml)
                                }
                                if ( map.test_deploy == 'ansible' ) {
                                    container('ansible') {
                                        dp.ansibleMvnHostDeploy(map.ansible_hosts_file, ansible_host, map.project_name, map.git_profile_path, map.apppath)
                                    }
                                }
                                if ( map.test_deploy == 'ssh' ) {
                                    dp.sshDockerDeploy(map.ssh_name, map.ssh_host, map.ssh_port, map.ssh_cred, map.dhub_cred, map.registry, image_name, deploy_path)
                                }
                                if ( map.test_deploy == 'nodeploy' ) {
                                    throw new Exception('您选择的环境未配置部署方式！！！,请联系运维人员处理')
                                }
                            }
                        }
                    }
                    stage('deploy pre') {
                        // pre环境部署
                        when {
                            expression { params.ENV_TYPE ==~ /(pre)/ }
                        // expression { BRANCH_TAG ==~ /(test)/ }
                        }
                        steps {
                            script {
                                echo "[INFO] pre 部署方式: $map.pre_deploy"
                                if ( map.pre_deploy == 'k8s' ) {
                                    dp.k8sDeploy(map.k8s_conn_yaml, params.ENV_TYPE, image_name, map.deploy_yaml)
                                }
                                if ( map.pre_deploy == 'ansible' ) {
                                    container('ansible') {
                                        dp.ansibleMvnDeploy(map.ansible_hosts_file, ansible_host, image_name, deploy_path)
                                    }
                                }
                                if ( map.pre_deploy == 'ssh' ) {
                                    dp.sshDockerDeploy(map.ssh_name, map.ssh_host, map.ssh_port, map.ssh_cred, map.dhub_cred, map.registry, image_name, deploy_path)
                                }
                                if ( map.pre_deploy == 'nodeploy' ) {
                                    throw new Exception('您选择的环境未配置部署方式！！！,请联系运维人员处理')
                                }
                            }
                        }
                    }
                    stage('deploy pro') {
                        // pro环境部署
                        when {
                            allOf {
                                expression { params.ENV_TYPE ==~ /(pro)/ }
                            }
                            anyOf {
                                expression { env.BUILD_USER == 'admin' }
                                expression { env.BUILD_USER == 'zhangyu' }
                                expression { env.BUILD_USER == 'jiangxiliang' }
                            }
                        }
                        steps {
                            script {
                                echo "[INFO] pro 部署方式: $map.pro_deploy"
                                if ( map.pro_deploy == 'k8s' ) {
                                    dp.k8sDeploy(map.k8s_conn_yaml, params.ENV_TYPE, image_name, map.deploy_yaml)
                                }
                                if ( map.pro_deploy == 'ansible' ) {
                                    container('ansible') {
                                        dp.ansibleMvnDeploy(map.ansible_hosts_file, ansible_host, image_name, deploy_path)
                                    }
                                }
                                if ( map.pro_deploy == 'ssh' ) {
                                    dp.sshDockerDeploy(map.ssh_name, map.ssh_host, map.ssh_port, map.ssh_cred, map.dhub_cred, map.registry, image_name, deploy_path)
                                }
                                if ( map.pre_deploy == 'nodeploy' ) {
                                    throw new Exception('您选择的环境未配置部署方式！！！,请联系运维人员处理')
                                }
                            }
                        }
                    }
                }
            }
        }
        post {
            // success {
            //     script {
            //         dm.DingdingReq(map.robotId, "构建成功 ✅")
            //     }
            // }
            failure {
                script {
                    dm.DingdingReq(map.robotId, '构建失败 ❌')
                }
            }
        }
    }
}

